{% extends "@CoreBundle/layout/pop.html.twig" %}
{% import "@CoreBundle/macros/bind_menu.html.twig" as macros %}

{% block body_custom_javascript %}
<script>
$(function(){
    $('#menu-tree').nestable({
        maxDepth:3,
        'onDragStart': function (l, e) {
                return false;
        }
    });
   $('.dd').nestable('collapseAll');
   
    $('.menu-tree-tools').on('click', function(e){
        var action = $(this).data('action');
        if (action === 'expand') {
            $('.dd').nestable('expandAll');
        }
        if (action === 'collapse') {
            $('.dd').nestable('collapseAll');
        }
    });

    $("#menuSelectAll").click(function(){
        var flag = this.checked;
        $("input[type='checkbox']").each(function(){
            // 将获取的状态赋值给当前对象，
            this.checked = flag;
        });
    });

    //单选
    $("input[type='checkbox']", "#menu-tree").each(function(){
        $(this).click(function(){
            var flag = this.checked;
            //父节点取消全部取消
            $("input[type='checkbox']", $(this).parents(".dd-item:first")).each(function(){
                // 将获取的状态赋值给当前对象，
                if(!flag) this.checked = flag;
            });

            //子节点选中，父节点选中
            $("input[type='checkbox']", $(this).parents(".dd-item:first").parents(".dd-item:first").find(".dd-handle:first")).each(function(){
                // 将获取的状态赋值给当前对象，
                if(flag) this.checked = flag;
            });

        });
    })

    //保存
    $("#menuBindSave").click(function(){
        var checkIdStr = "";
        $("input[type='checkbox']", "#menu-tree").each(function(){
            if(this.checked){
                checkIdStr += $(this).val()+",";
            }
        });
        checkIdStr = checkIdStr.substring(0, checkIdStr.lastIndexOf(','));
        var data = {data:checkIdStr};
        requestPost("{{ path('admin_api_role_bindmenu', {id:info['id']}) }}", data);
    });

})
</script>
{% endblock %}
{% block page %}
            <div class="card card-default">
                <div class="card-header">
                    <div class="btn-group">
                        <button class="btn btn-primary btn-sm menu-tree-tools" data-action="expand" title="展开">
                            <i class="mdi mdi-plus-box-outline"></i>&nbsp;展开
                        </button>
                        <button class="btn btn-primary btn-sm menu-tree-tools" data-action="collapse"
                                title="收起">
                            <i class="mdi mdi-minus-box-outline"></i>&nbsp;收起
                        </button>
                    </div>
                    {% if info['isLock'] ==0 %}
                        <div class="btn-group">
                            <button class="btn btn-info btn-sm" id="menuBindSave" title="保存">
                                <i class="mdi mdi-content-save"></i><span class="hidden-xs">&nbsp;保存</span>
                            </button>
                        </div>

                        <div class="icheck-primary d-inline pull-right">
                            <input type="checkbox" id="menuSelectAll" ><label for="menuSelectAll">全选</label>
                        </div>
                    {% endif %}
                </div>
                <div class="card-body table-responsive">
                    <div class="dd" id="menu-tree">
                        {{ macros.bind_menu(allMenu, 0, info, menuIds) }}
                    </div>
                </div>
            </div>
{% endblock %}
